import { observable, action } from 'mobx'
import axios from '../../../common/request'
import  notificationWithIcon from '../../common/notificationWithIcon';
import modalDisplay from '../../common/informModal'
import showConfirm from "../../common/confirmModal"

class EditActivityStore {
    @observable id
    @observable responseList=[]
    checkNum=3
    @observable tags=[]
    @observable industry=[]
    setTags=[]
    setIndustry=[]
    @observable activity={}
    actions = {
        getActivity:action((id)=>{
            axios.get('/api/activity/detail/'+id)
                .then((response)=> {
                    if(response.data.status.code===1){
                        let arr=[]
                        if(response.data.result.tags.length!=0){
                            response.data.result.tags.forEach((item)=>arr.push(item.id))
                            this.actions.setTags(arr)
                        }
                        if(response.data.result.industry!=null)
                            this.actions.setIndustry(response.data.result.industry.split(','))
                        response.data.result.tags=arr
                        this.activity=response.data.result
                    }
                    else if(response.data.status.code===2){
                        this.activity={}
                    }
                    else{
                        notificationWithIcon('error',response.data.status.message,"")
                    }
                })
                .catch(function (error) {
                    notificationWithIcon('error','获取活动信息失败',error.response)
                    console.log(error);
                });
        }),
        getTags:action(()=>{
            let tag=[]
            axios.get('/api/activity/params/tag')
                .then((response)=> {
                    if(response.data.status.code===1){
                        response.data.result.forEach((item)=>{
                            tag.push({label:item.name,value:item.id,disabled:false})
                        })
                        this.tags=tag
                        this.actions.getIndustry()
                    }
                    else{
                        notificationWithIcon('error',response.data.status.message,"")
                    }
                })
                .catch(function (error) {
                    notificationWithIcon('error','获取活动标签信息失败',error.response)
                    console.log(error);
                });
        }),
        getIndustry:action(()=>{
            let ind=[]
            axios.get('/api/activity/params/industry')
                .then((response)=> {
                    if(response.data.status.code===1){
                        response.data.result.forEach((item)=>{
                            ind.push({label:item.name,value:item.name,disabled:false})
                        })
                        this.industry=ind
                        this.actions.getActivity(this.id)
                    }
                    else{
                        notificationWithIcon('error',response.data.status.message,"")
                    }
                })
                .catch(function (error) {
                    notificationWithIcon('error','获取活动行业信息失败',error.response)
                    console.log(error);
                });
        }),
        setTags:action((value)=>{
            this.setTags=value;
            this.actions.setCheckBox(value,this.tags)
        }),
        setIndustry:action((value)=>{
            this.setIndustry=value;
            this.actions.setCheckBox(value,this.industry)
        }),
        setCheckBox: action((value,check)=>{
            if(value&&value.length===this.checkNum){
                check.forEach((item2)=>{
                    item2.disabled=true
                })
                value.forEach((item)=>{
                    check.forEach((item2)=>{
                        if(item2.value===item){
                            item2.disabled=false
                        }
                    })
                })
            }
            else{
                check.forEach((item2)=>{
                    item2.disabled=false
                })
            }
        }),
        launchActivity: action((activity,id,toggle,history)=>{
            const range=activity.rangeTimePicker
            activity.startTime=range[0]
            activity.endTime=range[1]
            activity.province=activity.address[0]
            activity.city=activity.address[1]
            activity.area=activity.address[2]
            activity.industry=this.setIndustry
            activity.tags=this.setTags
            activity.organizations=this.activity.organizations
            activity.guestInfo=this.activity.guestInfo
            activity.introduce=this.activity.schedule
            activity.schedule=this.activity.schedule
            activity.activityContent=this.activity.activityContent
            activity.activityGuide=this.activity.activityGuide
            activity.imagePath=this.activity.imagePath
            delete activity.address
            delete activity.rangeTimePicker
            delete activity.finalPath
            if(id==="-1"){
                console.log(activity)
                axios.post('/api/activity/add',activity)
                    .then((response)=> {
                        if(response.data.status.code===1){
                            toggle(false)
                            // notificationWithIcon('success',"发布活动成功","")
                            showConfirm("发布活动成功","是否发布下一条","check-circle-o",()=>{window.scrollTo(0,0);window.location.reload()},()=>history.push(`/console/activity/activityList/ing`),"是","否")
                            // modalDisplay("success","发布活动成功","",)
                        }
                        else{
                            toggle(false)
                            modalDisplay('error',response.data.status.message,"",null)
                        }
                    })
                    .catch(function (error) {
                        toggle(false)
                        modalDisplay('error','发布活动失败',error.response,null)
                        console.log(error);
                    });
            }
            else{
                activity.userId=this.activity.userId
                activity.id=this.activity.id
                axios.post('/api/activity/update',activity)
                    .then((response)=> {
                        if(response.data.status.code===1){
                            toggle(false)
                            modalDisplay('success',"修改活动成功","",()=>history.push(`/console/activity/activityList/ing`))
                        }
                        else{
                            toggle(false)
                            modalDisplay('error',response.data.status.message,"",null)
                        }
                    })
                    .catch(function (error) {
                        toggle(false)
                        modalDisplay('error','修改活动失败',error.response,null)
                        console.log(error);
                    });
            }
        })
    }
}
export default new EditActivityStore()